/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import Mensajes.Orden;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.sql.DataSource;
import javax.naming.InitialContext;

/**
 *
 * @author Lino
 */
public class conexionBD {

    private static String UserName = "root";
    private static String Pass = "root";
    private static String BD = "jdbc:mysql://localhost:8084/Laboratorio";

    public static int insert(Orden laOrden) {
        int salida = 0;

        try {

            int id = laOrden.getIdOrden();
            int osId = laOrden.getOsId();
            int indicId = laOrden.getIndicId();
            int estudioId = laOrden.getEstudioId();
            String documento = laOrden.getNroDocumento();
            String fechaHora = laOrden.getFechaHora();
            String msjHl7 = laOrden.getMsjHl7();

            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource datasource = (DataSource) envContext.lookup("jdbc/Laboratorio");
            java.sql.Connection conexion = datasource.getConnection();
            /*
             * InitialContext ctx = new InitialContext(); //The JDBC Data source
             * that we just created DataSource ds = (DataSource)
             * ctx.lookup("laboratorio"); java.sql.Connection conexion =
             * ds.getConnection();
             */
            java.sql.Statement s = (java.sql.Statement) conexion.createStatement();



            String query = "INSERT INTO Laboratorio.Orden "
                    + "(idOrden, "
                    + "OsId, "
                    + "IndicId, "
                    + "EstudioId, "
                    + "NroDocumento, "
                    + "FechaHora, "
                    + "msjHl7) "
                    + "VALUES(" + id + ", " + osId + ", " + indicId + ", " + estudioId
                    + ", " + documento + ", " + fechaHora + ", '" + msjHl7 + "');";

            //ResultSet rs = s.executeQuery(query);
            salida = s.executeUpdate(query);
            log("NuevaOrden", msjHl7, "Recibido Ok", id);

            conexion.close();
        } catch (Exception e) {
            e.printStackTrace();
            log("NuevaOrden", laOrden.getMsjHl7(), "Error al insertar nueva orde de laboratorio", laOrden.getIdOrden());
        }
        return salida;
    }

    public static int log(String tipo, String hl7, String resultado, int msjId) {
        int salida = 0;

        try {

            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource datasource = (DataSource) envContext.lookup("jdbc/Laboratorio");
            java.sql.Connection conexion = datasource.getConnection();

            java.sql.Statement s = (java.sql.Statement) conexion.createStatement();

            String query = "INSERT INTO laboratorio.LogWSLab "
                    + "(LogMsjTipo, "
                    + "LogResultado, "
                    + "LogMsjHl7, "
                    + "LogFechaHora,"
                    + "LogMsjId) "
                    + "VALUES ('" + tipo + "', '" + resultado + "', '" + hl7 + "', now(), " + msjId + ");";

            salida = s.executeUpdate(query);

            conexion.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return salida;
    }
}